【Mongodb搭建】

mongodb官网：https://www.mongodb.com/try
https://www.percona.com/downloads

搭建环境：openEuler 24.03 (LTS)
cat /etc/os-release |grep "^PRETTY_NAME"
PRETTY_NAME="openEuler 24.03 (LTS)"

mongo要选对版本，否则会提示缺依赖
这里安装的版本是：mongodb-linux-x86_64-rhel90-6.0.19.tgz


# 安装相关依赖
yum install -y libcurl cyrus-sasl-gssapi cyrus-sasl-plain

# 下载相关mongo版本
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel90-6.0.19.tgz

# 解压到/usr/local/
tar -xf mongodb-linux-x86_64-rhel90-6.0.19.tgz -C /usr/local/

# 创建mongo软连接
ln -s /usr/local/mongodb-linux-x86_64-rhel90-6.0.19/ /usr/local/mongodb

# 检查版本是否兼容，如果能正常输出以下内容代表兼容
/usr/local/mongodb/bin/mongod --version 

db version v6.0.19
Build Info: {
    "version": "6.0.19",
    "gitVersion": "a7ada5ff3a4d8a1e2ed88f86bd6b3d1d16cb43c6",
    "openSSLVersion": "OpenSSL 3.0.12 24 Oct 2023",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "rhel90",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

# 配置全局变量
vim /etc/profile

# 补充以下内容：
# MONGODB_HOME
export MONGODB_HOME=/usr/local/mongodb
# PATH
export PATH=$PATH:$MONGODB_HOME/bin

# 刷新生效
source /etc/profile

# 创建数据目录
mkdir -p /data/mongodb/{data,logs,conf,run}

# 生成密钥 - 【如果是配置集群，切记要使用同一个keyfile】
openssl rand -base64 756 > /data/mongodb/conf/keyfile
chmod 600 /data/mongodb/conf/keyfile

# 准备mongo配置文件
vim /data/mongodb/conf/mongod.conf

processManagement:
  fork: true
  pidFilePath: "/data/mongodb/run/mongod.pid"
systemLog:
    destination: file
    path: "/data/mongodb/logs/mongod.log"
    logAppend: true
    timeStampFormat: iso8601-local
storage:
    dbPath: "/data/mongodb/data"
    journal:
        enabled: true
    wiredTiger:
        engineConfig:
            cacheSizeGB: 8
        collectionConfig:
            blockCompressor: snappy
        indexConfig:
            prefixCompression: true
net:
    bindIp: 0.0.0.0
    port: 27017
security:
    authorization: enabled
    keyFile: "/data/mongodb/conf/keyfile"
operationProfiling:
    mode: slowOp
    slowOpThresholdMs: 2000
replication:
    oplogSizeMB: 20480
    replSetName: mongodb_suyuan

# 创建mongo用户
groupadd mongodb && useradd -g mongodb mongodb -M -s /sbin/nologin
chown mongodb:mongodb -R /data/mongodb/
chown mongodb:mongodb -R /usr/local/mongodb-linux-x86_64-rhel90-6.0.19/ /usr/local/mongodb

# 配置systemd启动
vim /etc/systemd/system/mongodb.service

[Unit]
Description=MongoDB Server
After=network.target remote-fs.target nss-lookup.target
[Service]
User=mongodb
Type=forking
ExecStart=/usr/local/mongodb/bin/mongod --config /data/mongodb/conf/mongod.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/usr/local/mongodb/bin/mongod --config /data/mongodb/conf/mongod.conf --shutdown
PrivateTmp=true
LimitCORE=infinity
LimitNOFILE=100000
LimitNPROC=100000
[Install]
WantedBy=multi-user.target

# 重载并启动mongo
systemctl daemon-reload
systemctl start mongodb.service
systemctl enable mongodb.service
systemctl status mongodb.service

# 下载mongosh连接工具
wget https://downloads.mongodb.com/compass/mongodb-mongosh-2.3.8.x86_64.rpm
rpm -ivh mongodb-mongosh-2.3.8.x86_64.rpm

# 初次登录mongo没有密码
mongosh

# 定义副本集配置
config = {
    _id : "mongodb_suyuan",
    members : [{_id : 0, host : "192.168.10.6:27017" }]
}
mongodb_suyuan 是定义的副本集名称，需要和mongod.conf配置里的保持一致
192.168.10.6:27017 是节点的连接地址

# 初始化副本集
rs.initiate(config)
提示 { ok: 1 } 代表成功，成功配置的副本集，登录显示的命令框会从 > 变成副本集名称 节点角色
test>
mongodb_suyuan [direct: primary] test> 

# 查看副本集状态
rs.status()

# 添加副本集从节点（如果是集群则需要配置）
rs.add("192.168.10.7:27017")

# 添加副本集仲裁节点（如果是集群可以配置，4台节点以上）
rs.addArb("192.168.10.8:27017")

# 创建root用户，并设置密码
use admin
db.createUser({user:"root",pwd:"mongo@2025",roles:[{role:'root',db:'admin'}]})

# 登录root用户
db.auth('root' , 'mongo@2025')

#查看用户信息
show users

# 其他
db.dropUser('testadmin') # 删除用户
db.updateUser('root', {pwd: 'admin456'}) # 修改用户密码